package ui.magic.magicui.container.Linear;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import ui.magic.magicui.container.Absolute.FVector;
import ui.magic.magicui.container.Base.MagicInterface;
import ui.magic.magicui.container.Base.MagicType;
import ui.magic.magicui.container.Base.Percent;

/**
 * Created by liushiyin on 2017/10/21.
 */

public class LinearMagicUi extends LinearMagicNull {

  /********************以下参数用于指定父节点中子节点的对齐方式*****************/
    public static int LEFT=Gravity.LEFT;
    public static int RIGHT=Gravity.RIGHT;
    public static int TOP=Gravity.TOP;
    public static int BOTTOM=Gravity.BOTTOM;
    public static int CENTER=Gravity.CENTER;
  /****************************************************************************/
    private Vector<View> viewVector;
    private Vector<Percent> viewPercent;
    private Vector<MagicInterface> magicViews;
    private Vector<Percent> magicPercents;
    private MagicType type;

    public LinearMagicUi(Context context) {
        super(context);
        init(MagicType.As_Child);
    }

    public LinearMagicUi(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(MagicType.As_Child);
    }

    public LinearMagicUi(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(MagicType.As_Child);
    }

    /*
    *设置字节点的对齐方式
    *@Param align：子节点的对齐方式，可选参数为LEFT、RIGHT、TOP、BOTTOM、CENTER，
    * 也可以通过“|”操作符结合使用
     */
    public void setAlign(int align){
        this.setGravity(align);
    }

    /*
  *进行一些初始化操作
   */
    private void init(MagicType type) {
        this.type=type;
        viewVector = new Vector<>();
        viewPercent=new Vector<>();
        magicViews=new Vector<>();
        magicPercents=new Vector<>();
        setMagicSize(getWindowSize().x,getWindowSize().y);
        setAlign(CENTER);
    }

    /*
   *在组件大小改变时，调整子节点大小
    */
    private void reOrderChildSize(){
        int count=viewVector.size();
        for(int i=0;i<count;++i){
            Percent percent=viewPercent.get(i);
            View v=viewVector.get(i);
            super.removeView(v);
            addChildViewTmp(v,percent.perx,percent.pery);
        }
        count=magicViews.size();
        for(int i=0;i<count;++i){
            Percent percent=viewPercent.get(i);
            MagicInterface magic=magicViews.get(i);
            super.removeView((View) magic);
            addMagicUiTmp(magic,percent.perx,percent.pery);
        }
    }

    /*
  *设置UI组件大小，同时会调整子组件大小，自己不要轻易调用此函数，
  *而应有UI组件自身在适当的时候调用
  *@Param width：宽度
  *@Param height：高度
   */
    @Override
    public void setMagicSize(int width, int height) {
        super.setMagicSize(width, height);
        reOrderChildSize();
    }

    /*
  *添加View子节点
  * @Param v：View子节点
  * @Param perWidth：0~100的浮点数表示子节点宽度为父节点宽度的百分比值
  * @Param perHeight：0~100的浮点数表示子节点宽度为父节点高度的百分比值
   */
    public void addChildView(View v, float perWidth, float perHeight){
        addChildViewTmp(v,perWidth,perHeight);
        viewVector.add(v);
        viewPercent.add(new Percent(perWidth,perHeight));
    }

    /*
  *添加MagicUi字节点，也即继承了MagicInterface的组件
  * @Param magic：MagicUi子节点
  * @Param perWidth：0~100的浮点数表示子节点宽度为父节点宽度的百分比值
  * @Param perHeight：0~100的浮点数表示子节点宽度为父节点高度的百分比值
    */
    public void addMagicUi(MagicInterface magic, float perWidth, float perHeight){
        addMagicUiTmp(magic,perWidth,perHeight);
        magicViews.add(magic);
        magicPercents.add(new Percent(perWidth,perHeight));
    }

    /*
   *addMagicUi的中间过程
    */
    private void addMagicUiTmp(MagicInterface magic, float perWidth, float perHeight){
        float dxw=perWidth/MAX;
        float dxh=perHeight/MAX;
        int realWidth=(int)(magicSize.x*dxw);
        int realHeight=(int)(magicSize.y*dxh);
        magic.setMagicSize(realWidth,realHeight);
        magic.setLocation(0,0);
        LayoutParams params=new LayoutParams(realWidth,realHeight);
        params.setMargins(0,0,0,0);
        super.addView((View) magic,params);
    }

    /*
    *addChildView的中间过程
     */
    private void addChildViewTmp(View v, float perWidth, float perHeight){
        float dxw=perWidth/MAX;
        float dxh=perHeight/MAX;
        int realWidth=(int)(magicSize.x*dxw);
        int realHeight=(int)(magicSize.y*dxh);
        LayoutParams params=new LayoutParams(realWidth,realHeight);
        params.setMargins(0,0,0,0);
        super.addView(v,params);
    }

    /*
   *移除指定的View字节点
   * @Param v：指定的view节点
   * @return boolean：成功返回true，失败返回false
    */
    public boolean removeChildView(View v) {
        if(viewVector.contains(v)) {
            super.removeView(v);
            int index = viewVector.indexOf(v);
            viewVector.remove(v);
            viewPercent.remove(index);
            return true;
        }else{
            return false;
        }
    }

    /*
   *移除指定的MagicUi子节点
   * @Param magic：想要移除的magic子节点
   * @return boolean：移除成功返回true，失败返回false
    */
    public boolean removeMagicUi(MagicInterface magic){
        if(magicViews.contains(magic)){
            super.removeView((View) magic);
            int index=magicViews.indexOf(magic);
            magicViews.remove(magic);
            magicPercents.remove(index);
            return true;
        }else{
            return false;
        }
    }

    public Percent getChildPerInfo(View child){
        if(viewVector.contains(child)){
            int index=viewVector.indexOf(child);
            return viewPercent.get(index);
        }else{
            if(magicPercents.contains(child)){
                int index=magicViews.indexOf(child);
                return magicPercents.get(index);
            }else{
                return null;
            }
        }
    }

    public boolean changeChildSize(View child,float perWidth,float perHeight){
        if(viewVector.contains(child)){
            int index=viewVector.indexOf(child);
            Percent percent=viewPercent.get(index);
            percent.perx=perWidth;
            percent.pery=perHeight;
            super.removeView(child);
            addChildViewTmp(child,perWidth,perHeight);
            return true;
        }else{
            if(magicViews.contains(child)){
                int index=magicViews.indexOf(child);
                Percent percent=magicPercents.get(index);
                percent.perx=perWidth;
                percent.pery=perHeight;
                super.removeView(child);
                addMagicUiTmp((MagicInterface) child,perWidth,perHeight);
                return true;
            }else{
                return false;
            }
        }
    }

    /*
    *设置子节点的Margin
     */
    public void setChildMargin(View child,int left,int top,int right,int bottom){
        LayoutParams params=(LayoutParams) child.getLayoutParams();
        params.setMargins(left,top,right,bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(type==MagicType.Self_Init){
            int width=getMeasuredWidth();
            int height=getMeasuredHeight();
            if(magicSize.x!=width||magicSize.y!=height){
                setMagicSize(width,height);
            }
        }
    }

    public void setSelfInit(){
        type=MagicType.Self_Init;
    }

    public void setAsChild(){
        type=MagicType.As_Child;
    }

    /*
    *设置布局方式为水平方向
     */
    public void setHorizontal(){
        this.setOrientation(LinearLayout.HORIZONTAL);
    }

    /*
    *设置布局方式为垂直方式
     */
    public void setVertical(){
        this.setOrientation(LinearLayout.VERTICAL);
    }

    //一下函数标记为废弃，不建议使用
    @Override
    @Deprecated
    public void addView(View child) {super.addView(child);}
    @Override
    @Deprecated
    public void addView(View child, int index) {super.addView(child, index);}
    @Override
    @Deprecated
    public void addView(View child, int width, int height) {super.addView(child, width, height);}
    @Override
    @Deprecated
    public void addView(View child, ViewGroup.LayoutParams params) {super.addView(child, params);}
    @Override
    @Deprecated
    public void addView(View child, int index, ViewGroup.LayoutParams params) {super.addView(child, index, params);}
    @Override
    @Deprecated
    public void removeView(View view) {super.removeView(view);}
    @Override
    @Deprecated
    public void removeViewAt(int index) {super.removeViewAt(index);}
    @Override
    @Deprecated
    public void removeViews(int start, int count) {super.removeViews(start, count);}
    @Override
    @Deprecated
    public void removeAllViews() {super.removeAllViews();}
}
